﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace AGuang.WebTool {
    /// <summary>
    /// 操作实体类的服务
    /// </summary>
    public class ModelsService {
        /// <summary>
        /// 将类的属性值拷给另一个同名属性
        /// </summary>
        /// <param name="source">源</param>
        /// <param name="target">目标</param>
        public void Copy(Object source, Object target) {
            if (source == null) throw new ArgumentNullException("source");
            if (target == null) throw new ArgumentNullException("target");
            Type sourceType = source.GetType();
            Type targetType = target.GetType();
            PropertyInfo[] sourcePropertis = sourceType.GetProperties();
            foreach (PropertyInfo sourceProperty in sourcePropertis) {
                if (sourceProperty.CanRead) {
                    PropertyInfo targetProperty = targetType.GetProperty(sourceProperty.Name);
                    if (targetProperty == null) continue;
                    if (!targetProperty.CanWrite) continue;
                    if (sourceProperty.GetIndexParameters().GetLength(0) == 0 && targetProperty.GetIndexParameters().GetLength(0) == 0) {
                        Object value = sourceProperty.GetValue(source, null);
                        targetProperty.SetValue(target, value, null);
                    }
                }
            }
        }
    }
}
